import matplotlib.pyplot as plt
import numpy as np

n_min = -2
n_max = 3
n = np.arange(n_min, n_max+1, 1)


def x1(t):

    v0 = [1 if (0 <= i <= 3) else 0 for i in t]
    v1 = [-1 / 2 if (i == -1) else 0 for i in t]
    v2 = [1 / 2 if (i == -2) else 0 for i in t]
    v3 = [0 if (i < -2 or i > 3) else 0 for i in t]

    y = v0 * t / t + v1 + v2 + v3

    return y


y1 = x1(4 - n)

plt.rcParams['font.sans-serif'] = ['SimHei']  # 加上这一句就能在图表中显示中文
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

plt.subplot(411)
plt.stem(n, y1)
plt.ylabel('x1')

plt.subplot(413)
plt.stem(n, [0, 0, 0, 0, 0, 1])
plt.ylabel('x2')
plt.show()
